﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lg.Core.Form.Types
{
    public class BaseType
    {
        /// <summary>
        /// 字段标题
        /// </summary>
        public string? Title { get; set; }
        /// <summary>
        /// 英文标题
        /// </summary>
        public string? EnTitle { get; set; }
        /// <summary>
        /// 字段描述
        /// </summary>
        public string? Intro { get; set; }
        /// <summary>
        /// 字段默认值,多值分割符为"~"
        /// </summary>
        public string? Value { get; set; }
        /// <summary>
        /// 字段名(虚拟字段名-随机生成不重复)
        /// </summary>
        public string? Name { get; set; }
        /// <summary>
        /// 导出列名
        /// </summary>
        public string? ExportName { get; set; }
        /// <summary>
        /// 字段宽度(%)
        /// </summary>
        public string? Width { get; set; }
        /// <summary>
        /// 是否必填
        /// </summary>
        public bool Required { get; set; }
        /// <summary>
        ///填写提示
        /// </summary>
        public string? Tips { get; set; }

        public bool IsWhere { get; set; }
        public bool IsExport { get; set; }
        public bool IsRender { get; set; }
        public bool IsShow { get; set; }
        /// <summary>
        /// 查询关键字
        /// </summary>
        public string? WhereKey { get; set; }
        public string? Group { get; set; }

        #region [基类方法]

        public virtual FormData? SetValue(JObject formData)
        {
            string? value = formData[this.Name!]?.ToString();
            this.Value = value ?? "";
            if (this.Value == "lg-no-render")
            {
                this.IsRender = false;
                this.Value = "";
            }
            else
            {
                this.Value = value;
            }
            return this.GetValue();
        }
        public virtual FormData GetValue()
        {
            return new FormData
            {
                Name = Name,
                Value = Value,
                Title = Title,
                ExportName = ExportName
            };
        }

        public virtual List<FormData> GetValue(List<FormData> list)
        {
            list.Add(this.GetValue());
            return list;
        }

        public virtual string? FormatValue(string value)
        {
            if (string.IsNullOrEmpty(value) || value == "lg-no-render")
            {
                return "";
            }
            return value;
        }
        public virtual List<FormData> FormatValue(List<FormData> list, JObject formData)
        {
            string? value = formData[this.Name!]?.ToString();
            if (value == "lg-no-render")
            {
                value = "";
            }
            list.Add(new FormData { Name = Name, Value = value, Title = Title, ExportName = ExportName });
            return list;
        }
        #endregion

    }
}
